package com.cykj.pos.service.impl;

import com.cykj.common.core.domain.AjaxResult;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.apache.commons.lang3.StringUtils;
import com.cykj.pos.mapper.BizManufacturerMapper;
import com.cykj.pos.domain.BizManufacturer;
import com.cykj.pos.service.IBizManufacturerService;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 制造商Service业务层处理
 *
 * @author Zephyr
 * @date 2025-05-28
 */
@Service
public class BizManufacturerServiceImpl extends ServiceImpl<BizManufacturerMapper, BizManufacturer> implements IBizManufacturerService {

    @Resource
    private BizManufacturerMapper bizManufacturerMapper;

    @Override
    public List<BizManufacturer> queryList(BizManufacturer bizManufacturer) {
//        LambdaQueryWrapper<BizManufacturer> lqw = Wrappers.lambdaQuery();
//        if (StringUtils.isNotBlank(bizManufacturer.getManufacturerName())){
//            lqw.like(BizManufacturer::getManufacturerName ,bizManufacturer.getManufacturerName());
//        }
//        return this.list(lqw);
        return bizManufacturerMapper.queryManAndModel(bizManufacturer);
    }

    @Override
    public BizManufacturer getInfoById(Integer id,Integer mId) {
        return bizManufacturerMapper.getInfoById(id,mId);
    }

    @Override
    public AjaxResult addBizManufacturer(BizManufacturer bizManufacturer) {
        String name = bizManufacturer.getManufacturerName().trim();
        Integer id = bizManufacturerMapper.checkNameId(name);
        int modelId ;
        if(id == null){
            bizManufacturerMapper.insertDate(bizManufacturer);//存品牌
            //chaxun id
            Integer id_new = bizManufacturerMapper.selectByName(name);
            modelId = id_new;
        }else{
            modelId = id;
        }
        bizManufacturer.setMId(modelId);
        Integer model_id = bizManufacturerMapper.selectModelId(bizManufacturer);
        if(model_id == null){
            bizManufacturerMapper.insertModelData(bizManufacturer);//存型号
            model_id = bizManufacturerMapper.selectModelId(bizManufacturer);
        }
        //存押金
        bizManufacturerMapper.insertServiceFee(model_id,bizManufacturer.getModel()+"-0");
        bizManufacturerMapper.insertServiceFee(model_id,bizManufacturer.getModel()+"-99");
        bizManufacturerMapper.insertServiceFee(model_id,bizManufacturer.getModel()+"-199");
        bizManufacturerMapper.insertServiceFee(model_id,bizManufacturer.getModel()+"-299");

        return AjaxResult.success("保存成功");
    }

    @Override
    public AjaxResult updateBizManufacturer(BizManufacturer bizManufacturer) {
        bizManufacturerMapper.updateDate(bizManufacturer);
        bizManufacturerMapper.updateModelData(bizManufacturer);
        //删除押金
        bizManufacturerMapper.deleteByMId(bizManufacturer.getMId());
        //存押金
        bizManufacturerMapper.insertServiceFee(bizManufacturer.getMId(),bizManufacturer.getModel()+"-0");
        bizManufacturerMapper.insertServiceFee(bizManufacturer.getMId(),bizManufacturer.getModel()+"-99");
        bizManufacturerMapper.insertServiceFee(bizManufacturer.getMId(),bizManufacturer.getModel()+"-199");
        bizManufacturerMapper.insertServiceFee(bizManufacturer.getMId(),bizManufacturer.getModel()+"-299");
        return AjaxResult.success("修改成功");
    }
}
